﻿using CMS.Dialogs;
using CMS.Dialogs.Notifications;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CMS.Controls.TabPageTemplate
{
    public abstract class ChartExportTabPage : Panel
    {
        protected FormPanel FP_Main;
        protected Button Bn_Start;

        /// <summary>
        /// 消息发布者,用于输出LOG
        /// </summary>
        protected INoticePublisher _Publisher { get; private set; }


        /// <summary>
        /// 主按钮名称
        /// </summary>
        protected abstract string MainButtonName { get; }

        /// <summary>
        /// 导出名称
        /// </summary>
        protected abstract string ExportName { get; }


        public ChartExportTabPage()
        {
            _InitializeComponent();
        }

        private void _InitializeComponent()
        {
            SuspendLayout();
            BackColor = Color.White;
            FP_Main = new FormPanel
            {
                Location = new Point(40, 50),
                Size = new Size(400, 300)
            };

            FormBase.LayoutDetial layout = FP_Main.LayoutD;

            layout.RowHeight = 50;
            layout.Columns[0].Width = 100;
            layout.Columns[1].Width = 250;
            layout.Columns[2].Width = 80;

            Bn_Start = new Button()
            {
                Text = MainButtonName,
                Location = new Point(180, 400),
                Size = new Size(100, 30),
            };
            Bn_Start.Click += Bn_Start_Click;

            Controls.Add(FP_Main);
            Controls.Add(Bn_Start);
            FP_Main.SuspendLayout();
            _InitFormPanel(FP_Main);
            FP_Main.ResumeLayout();
            ResumeLayout();
        }


        private void Bn_Start_Click(object sender, System.EventArgs e)
        {

            _Publisher = ProcessNotificationDialog.Start(ExportName, null);

            try
            {
                _Publisher.WriteLine("INFO", "开始导出");
                StartExport();
                _Publisher.WriteLine("INFO", "导出结束");
            }
            catch (Exception err)
            {
                _Publisher.WriteLine("ERROR", "导出任务出错:" + err.Message);
            }
        }

        protected abstract void _InitFormPanel(FormPanel fp);


        /// <summary>
        /// 界面上的开始导出按钮一旦被点击,则立即调用此接口
        /// </summary>
        protected abstract void StartExport();
    }
}
